package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.alibaba.druid.sql.visitor.functions.If;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    //结算单对象
    @Autowired
    private StatementMapper statementMapper;

    //预约单对象
    @Autowired
    private AppointmentMapper appointmentMapper;

    //明细表对象
    @Autowired
    private StatementItemMapper statementItemMapper;

    //服务单项对象
    @Autowired
    private IServiceItemService serviceItemService;

    @Autowired
    private IAppointmentService appointmentService;


    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {

        //新增的数据不完整，需要额外处理
        //结算状态,需要我们取设置

            //创建时间
            statement.setCreateTime(new Date());
            //结算状态  消费中
            statement.setStatus(Statement.STATUS_CONSUME);

            statementMapper.insert(statement);

    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {

        //业务逻辑判断，只有在消费中的状态才可修改，其他状态不能编辑
        //当结算当状态为消费中， 编辑，明细，删除可用
        Statement oldStatement = statementMapper.selectByPrimaryKey(statement.getId());

        if (!Statement.STATUS_CONSUME.equals(oldStatement.getStatus())){
            //不是消费状态中，抛出异常
            throw new BusinessException("只有用户在消费状态下，才能编辑");
        }
        //带取的参数不完整，需要处理，在后端SQL处理
        statementMapper.update(statement);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public void updateAmount(Long statementId, BigDecimal itemPrice, BigDecimal totalAmount, BigDecimal totalCount) {
        statementMapper.updateAmount(statementId,itemPrice,totalAmount,totalCount);
    }

    @Override
    public void updateByStatementId(Long statementId) {

        //进行业务逻辑判断，只有在消费中的状态才可以进行支付
        Statement statement = statementMapper.selectByPrimaryKey(statementId);

        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            //抛出异常
            throw new BusinessException("请不要重复支付");
        }

        //修改状态     收款id，收款时间，状态
        //已支付
        statement.setStatus(Statement.STATUS_PAID);

        //收款时间
        statement.setPayTime(new Date());

        //收款人id
        statement.setPayeeId(ShiroUtils.getUserId());

        statementMapper.updateByStatementId(statement);

    }

    @Override
    public Statement generateStatement(Long id) {

        //根据id，获取预约单对象，获取状态并判断
        Appointment appointment = appointmentService.get(id);

        Statement statement = new Statement();

        //判断状态是否为到店状态，是的话，创建结算表对象，否则，根据预约单id，去查询结算表的信息
        if (Appointment.STATUS_ARRIVAL.equals(appointment.getStatus())) {
            //到店状态，创建结算表对象

            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setServiceType(appointment.getServiceType()+0L);      //转换为Long类型
            statement.setAppointmentId(appointment.getId());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setInfo(appointment.getInfo());
            statement.setCreateTime(new Date());
            //默认状态为消费中
            statement.setStatus(Statement.STATUS_CONSUME);

            statementMapper.insert(statement);

            //要去预约表中修改状态，结算单生成
            appointmentService.changeStatus(id,Appointment.STATUS_SETTLE);
        }else {

            //查询，因为是结算单生成的状态，根据预约单id，查询对应的结算单对象，返回即可
             statement = statementMapper.queryByAppointmentId(id);
        }

        return statement;
    }



    @Override
    public void remove(Long id) {

        //根据结算单id，查询结算对象，通过结算对象获取预约id,如果为空，不修改预约状态
        //如果预约id，不为空，进行删除，需要删除结算表和明细表的数据
        //问题：为什么要根据预约id不为空进行删除

        Statement statement = statementMapper.selectByPrimaryKey(id);

        //判断不为空的状态下，去进行修改预约单的状态
        if (statement.getAppointmentId() != null) {
            appointmentMapper.changeStatus(statement.getAppointmentId(),Appointment.STATUS_CANCEL);
        }

        //根据传过来的结算表id，删除数据
        statementMapper.deleteByPrimaryKey(statement.getId());

        //明细表要删除
        statementItemMapper.deleteByStatementId(statement.getId());


    }


}